var areainfo    = '';
var endFlag     = "$";
var arealevel   = 0;
var init        = true;

function initArea(level) {
    if (level < 0 || level > 3) {
        return false;
    } else {
        var selectGroup = '';
        arealevel = level;
        if (level >= 1) {
            selectGroup += '<select id="province" onchange="changeProvince()" class="select_area">';
            selectGroup += '<option id="-1" value="0">请选择省</option>';
            selectGroup += '</select>';
        }

        if (level >= 2) {
            selectGroup += '<select id="city" onChange="changeCity()" class="select_area">';
            selectGroup += '<option id="-2" value="0">请选择市</option>';
            selectGroup += '</select>';
        }

        if (level == 3) {
            selectGroup += '<select id="area" onchange="changeArea()" class="select_area">';
            selectGroup += '<option id="-3" value="0">请选择区</option>';
            selectGroup += '</select>';
        }

        $('#areaInfo').append(selectGroup);

        if (level == 1) {
            select_width = '100%';
        } else if (level == 2) {
            select_width = '50%';
        } else {
            select_width = '33%';
        }
        $('.select_area').css('width', select_width);

        var option = '';
        for (var i in area) {
            if(area[i].level == 1) {
                option += "<option value='" + area[i].id + "'>" + area[i].areaname + "</option>";
            }
        }
        $('#province').append(option);

        // 初始化
        if (typeof(areaPath) != 'undefined') {
            if (areaPath[0]) {
                $('#province').find("option[value=" + areaPath[0] + "]").attr('selected', 'selected');
                changeProvince();
            }
            if (areaPath[1]) {
                $('#city').find("option[value=" + areaPath[1] + "]").attr('selected', 'selected');
                changeCity();
            }
            if (areaPath[2]) {
                $('#area').find("option[value=" + areaPath[2] + "]").attr('selected', 'selected');
                changeArea();
            }
        }
        init = false;
        return true;
    }
}

function changeProvince() {
    var hasChild    = false;
    var option      = '<option id="-2" value="0">请选择市</option>';
    var province_id = $('#province').val();
    areainfo        = province_id;
    for (var i in area) {
        if(area[i].parentid == province_id) {
            option      += "<option value='" + area[i].id + "'>" + area[i].areaname + "</option>";
            hasChild    = true;
        }
    }
    if ((!hasChild) || (arealevel == 1)) {
        areainfo += endFlag;
    }
    $('#city').empty();
    $('#city').append(option);
    $('#area').empty();
    $('#area').append('<option value="0">请选择区</option>');

    setAreaValue();
}

function changeCity() {
    var hasChild    = false;
    var option      = '<option id="-3" value="0">请选择区</option>';
    var city_id     = $('#city').val();
    areainfo        = $('#province').val() + "_" + city_id;
    for (var i in area) {
        if(area[i].parentid == city_id) {
            option      += "<option value='" + area[i].id + "'>" + area[i].areaname + "</option>";
            hasChild    = true;
        }
    }
    if ((!hasChild) || (arealevel == 2)) {
        areainfo += endFlag;
    }
    $('#area').empty();
    $('#area').append(option);

    setAreaValue();
}

function changeArea() {
    areainfo    = $('#province').val() + "_" + $('#city').val() + "_" + $('#area').val();
    areainfo    += endFlag;

    setAreaValue();
}

function getAreaInfo() {
    if (isAreaInfoValid()) {
        return areainfo.slice(0, areainfo.length - 1);
    } else {
        return 0;
    }
}

function isAreaInfoValid() {
    var valid = false;
    if (endFlag == areainfo.slice(-1)) {
        valid = true;
    }
    return valid;
}

// 设置
function setAreaValue() {
    if (isAreaInfoValid()) {
        if (arealevel == 1) {
            $('#areaInfo input').val($('#province').val());
        }

        if (arealevel == 2) {
            $('#areaInfo input').val($('#city').val());
        }

        if (arealevel == 3) {
            $('#areaInfo input').val($('#area').val());
        }

        if (typeof select_area_callback === "function") {
            if (!init) {
                select_area_callback();
            }
        }
    } else {
        $('#areaInfo input').val(0);
    }
}
